﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using cmsservice.DAL;
using cmsservice.Entity;
using System.IO;

namespace cmsservice.BLL
{
    public class StockWeatherBLL
    {
        /// <summary>
        /// 私有构造方法
        /// </summary>
        private StockWeatherBLL() { }
        /// <summary>
        /// 定义一个不稳定的对象
        /// </summary>
        private static volatile StockWeatherBLL instance = null;
        /// <summary>
        /// 加锁
        /// </summary>
        private static object locker = new object();
        /// <summary>
        /// 获取实例化对象
        /// </summary>
        /// <returns></returns>
        public static StockWeatherBLL StockWeatherInstance()
        {
            if (instance == null)
            {
                lock (locker)
                {
                    if (instance == null)
                        instance = new StockWeatherBLL();
                }
            }
            return instance;
        }

        readonly StockWeatherDAL dal = StockWeatherDAL.StockWeatherInstance();

        /// <summary>
        /// 获取股市天气预报
        /// </summary>
        /// <returns></returns>
        public string GetStockWeather(string bkqr)
        {
            WeatherData wd = new WeatherData();
            DataRow row = dal.GetStockWeather();
            if (row != null)
            {
                string date = DAL.Tools.GetString(row["date"]);
                wd.weather = DAL.Tools.GetString(row["weather"]);
                wd.posi = DAL.Tools.GetString(row["posi"]);
                wd.short_strategy = DAL.Tools.GetString(row["short_strategy"]);
                wd.long_strategy = DAL.Tools.GetString(row["long_strategy"]);
                wd.content = DAL.Tools.GetString(row["content"]);
                wd.date = Convert.ToDateTime(date.Substring(0, 4) + "-" + date.Substring(4, 2) + "-" + date.Substring(6, 2));
                wd.bkqr = bkqr == "" ? "暂无数据" : bkqr;
                wd.long_strategy = wd.bkqr;
            }
            string str = "";
            try
            {
                if (wd != null)
                {
                    string _path = "D:\\cfile\\EmoneyStockData\\StockWeathers" + wd.sdate + ".txt";
                    str = GetStockWeatherByTXT(_path, wd);
                }
            }
            catch
            {
            }
            return str;
        }

        /// <summary>
        /// 验证是否更新并存文件
        /// </summary>
        /// <param name="_path"></param>
        /// <returns></returns>
        private string GetStockWeatherByTXT(string _path, WeatherData wd)
        {
            string str = "";
            if (File.Exists(_path))
            {
                using (FileStream fs = File.Open(_path, FileMode.Open))
                {
                    using (StreamReader reader = new StreamReader(fs, Encoding.Default))
                    {
                        str = reader.ReadLine();
                        reader.Close();
                        WeatherData nwd = Newtonsoft.Json.JsonConvert.DeserializeObject<WeatherData>(str);
                        wd.ID = nwd.ID;
                        string nstr = Newtonsoft.Json.JsonConvert.SerializeObject(wd);
                        if (str != nstr)
                        {
                            wd.ID = (Convert.ToInt32(wd.ID) + 1).ToString();
                            str = Newtonsoft.Json.JsonConvert.SerializeObject(wd);
                        }
                        fs.Close();
                        fs.Dispose();
                    }
                }
            }
            else
            {
                wd.ID = wd.sdate.Substring(2, 6) + "001";
                str = Newtonsoft.Json.JsonConvert.SerializeObject(wd);
            }
            using (StreamWriter sw = new StreamWriter(_path, false, Encoding.UTF8))
            {
                sw.Write(str);
                sw.Close();
            }

            return str;
        }
    }
}
